package com.pig.graduate.service.impl.produce;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pig.graduate.common.exception.MyException;
import com.pig.graduate.domain.event.EventEntry;
import com.pig.graduate.domain.event.EventExit;
import com.pig.graduate.domain.event.EventTransferDormitory;
import com.pig.graduate.domain.finance.FinanceInfo;
import com.pig.graduate.domain.pigFarm.SowFarm;
import com.pig.graduate.domain.pigFarm.dto.BoarStateDTO;
import com.pig.graduate.mapper.event.EventEntryMapper;
import com.pig.graduate.mapper.event.EventExitMapper;
import com.pig.graduate.mapper.event.EventTransferDormitoryMapper;
import com.pig.graduate.mapper.finance.FinanceInfoMapper;
import com.pig.graduate.mapper.produce.ProduceBoarMapper;
import com.pig.graduate.domain.produce.ProduceBoar;
import com.pig.graduate.result.MyResult;
import com.pig.graduate.service.produce.ProduceBoarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (ProduceBoar)表服务实现类
 *
 * @author makejava
 * @since 2023-02-28 14:14:40
 */
@Service("produceBoarService")
public class ProduceBoarServiceImpl extends ServiceImpl<ProduceBoarMapper, ProduceBoar> implements ProduceBoarService {

    @Autowired
    private ProduceBoarMapper produceBoarMapper;

    @Autowired
    private EventEntryMapper eventEntryMapper;

    @Autowired
    private EventTransferDormitoryMapper eventTransferDormitoryMapper;

    @Autowired
    private EventExitMapper eventExitMapper;

    @Autowired
    private FinanceInfoMapper financeInfoMapper;

    @Override
    public MyResult boarList(ProduceBoar produceBoar) {
        PageHelper.startPage(produceBoar.getPageNum(), produceBoar.getPageSize());
        List<ProduceBoar> list = produceBoarMapper.boarList(produceBoar);
        for (ProduceBoar boar : list) {
            String location = boar.getBuildsName() + "-" + boar.getColumnName();
            boar.setLocation(location);
            if(boar.getDateOfBirth() != null){
                long betweenBirthDay = DateUtil.between(boar.getDateOfBirth(), new Date(), DateUnit.DAY);
                boar.setBirthDay(betweenBirthDay);
            }
            if(boar.getStateDate() != null){
                long betweenStateDay = DateUtil.between(boar.getStateDate(), new Date(), DateUnit.DAY);
                boar.setStateDay(betweenStateDay);
            }
        }
        PageInfo<ProduceBoar> pageInfo = new PageInfo<>(list);
        return MyResult.success(pageInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public MyResult boarEntry(ProduceBoar produceBoar) {
        ProduceBoar entity = produceBoarMapper.selectOne(new QueryWrapper<ProduceBoar>().eq("ear_code",produceBoar.getEarCode()));
        if(entity != null){
            throw new MyException("耳号已存在");
        }
        produceBoar.setOperatorsUser(produceBoar.getOperatorsUser());
        produceBoar.setCreateTime(new Date());
        produceBoar.setIsPresent("在场"); //表示在场
        produceBoarMapper.insert(produceBoar);
        EventEntry eventEntry = new EventEntry();
        eventEntry.setEntryDate(produceBoar.getEntryDate());
        eventEntry.setEntryType(produceBoar.getEntryType());
        eventEntry.setBuildingsId(produceBoar.getBuildingsId());
        eventEntry.setColumnId(produceBoar.getColumnId());
        eventEntry.setOperatorsUser(produceBoar.getOperatorsUser());
        eventEntry.setCreateTime(new Date());
        eventEntry.setEarCode(produceBoar.getEarCode());
        eventEntry.setEntryPigType(0);
        eventEntry.setRemark(produceBoar.getRemark());
        eventEntryMapper.insert(eventEntry);
        if(produceBoar.getPigType().equals("0")){ //1是母猪
            FinanceInfo financeInfo = new FinanceInfo();
            financeInfo.setName("猪只购买成本");
            financeInfo.setCreateTime(new Date());
            financeInfo.setSummary("猪只购买 耳号："+produceBoar.getEarCode());
            financeInfo.setPrice(produceBoar.getPrice().multiply(new BigDecimal(-1)));
            financeInfo.setTotalPrice(produceBoar.getPrice().multiply(new BigDecimal(-1)));
            financeInfo.setDataType("猪只购买");
            financeInfo.setIncomeExpensesType("支出");
            financeInfo.setNumber(1);
            financeInfoMapper.insert(financeInfo);
        }
        return MyResult.success("操作成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public MyResult boarMoveBuildings(EventTransferDormitory eventTransferDormitory) {
        List<ProduceBoar> list = produceBoarMapper.selectBatchIds(eventTransferDormitory.getIds());
        for (ProduceBoar produceBoar : list) {
            if(produceBoar.getColumnId().equals(eventTransferDormitory.getColumnIn())){
                throw new MyException("耳号为 "+produceBoar.getEarCode()+" 的种猪新转入栏与原栏相同");
            }
            EventTransferDormitory transferDormitory = new EventTransferDormitory();
            transferDormitory.setTdDate(eventTransferDormitory.getTdDate());
            transferDormitory.setEarCode(produceBoar.getEarCode());
            transferDormitory.setColumnOut(produceBoar.getColumnId());
            transferDormitory.setColumnIn(eventTransferDormitory.getColumnIn());
            transferDormitory.setRemark(eventTransferDormitory.getRemark());
            transferDormitory.setOperatorsUser(eventTransferDormitory.getOperatorsUser());
            transferDormitory.setTdPigType(0);//表示种猪
            eventTransferDormitoryMapper.insert(transferDormitory);
            produceBoar.setColumnId(eventTransferDormitory.getColumnIn());
            produceBoarMapper.updateById(produceBoar);
        }
        return MyResult.success("转舍成功");
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public MyResult boarLeave(EventExit eventExit) {
        List<ProduceBoar> list = produceBoarMapper.selectBatchIds(eventExit.getIds());
        for (ProduceBoar produceBoar : list) {
            eventExit.setEarCode(produceBoar.getEarCode());
            eventExit.setNumber(1);
            eventExit.setExitPigType(0);//表示种猪
            eventExitMapper.insert(eventExit);
            produceBoar.setIsPresent("离场");
            produceBoarMapper.updateById(produceBoar);
            FinanceInfo financeInfo = new FinanceInfo();
            financeInfo.setNumber(1);
            financeInfo.setCreateTime(new Date());
            financeInfo.setName("猪只销售收入");
            financeInfo.setSummary("猪只销售 耳号："+produceBoar.getEarCode());
            financeInfo.setPrice(eventExit.getUnitPrice());
            financeInfo.setTotalPrice(eventExit.getPrice());
            financeInfo.setDataType("猪只销售");
            financeInfo.setIncomeExpensesType("收入");
            financeInfoMapper.insert(financeInfo);
        }
        return MyResult.success("离场成功");
    }

    @Override
    public List<SowFarm> getSowFarm() {
        return produceBoarMapper.getSowFarm();
    }

    @Override
    public List<BoarStateDTO> getBoarForState() {
        return produceBoarMapper.getBoarForState();
    }


}

